
package com.scalar.chiptrack.operations.process.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.scalar.ScalarException;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.operations.process.ProcessStep;
import com.scalar.chiptrack.operations.process.SortBins;
import com.scalar.chiptrack.operations.process.SortProcess;
import com.scalar.chiptrack.operations.process.Upload;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.utils.SqlQueryMappings;


/**
 * Implementation of <strong>DAO</strong> business logic for
 *
 *
 * @author Kesava Kumar Achanala
 * @version $Revision: 1.1.1.1 $ $Date: 2006/12/11 23:43:10 $
 */


public class SortProcessDAO
{

   // ----------------------------------------------------- Instance Variables
   
   /**
    * Default Constructor
    *
    */
   public SortProcessDAO()
   {

   }


   // --------------------------------------------------------- Public Methods


  /**
   * Get Sort Process Information
   *
   * @param baseProductId
   * @param fabProductName
   */
   public static SortProcess getSortProcessInfo(String baseProductId, String previousStepId,
                    String processType, String trackId, String fabProductName) throws ScalarException {
       
       //Load the Previous Sort process info if it is any ???
       SortProcess sortProcess = null;
       //System.out.println("From process track : base product Id" + baseProductId );
       //System.out.println("From process track : PreviousStep Id" + previousStepId );
       //System.out.println("From process track : processType" + processType);
       //System.out.println("From process track : track Id" + trackId );
       //System.out.println("From process track : Fab Product Name" + fabProductName );
       try {
            int baseProductNo = -1;
            int previousStepNo = -1;
            int trackNo = -1;
            
            String processStepType = null;
            if(baseProductId != null)
                baseProductNo = Integer.parseInt(baseProductId);
            if(trackId != null)
                trackNo = Integer.parseInt(trackId);
            
            //Get the previous step type, if the sort information is already there, then populate
            if(previousStepId != null) {
                previousStepNo = Integer.parseInt(previousStepId);
                
                sortProcess = getPreviousSortProcess(previousStepNo); 
                
            }
                               
            if (sortProcess == null) {
                sortProcess = new SortProcess();
                            
                sortProcess.setVendorLocations(VendorDAO.getSortLocations());
                
            } else {
                sortProcess.setVendorLocations(
                        VendorDAO.getSortLocations(previousStepNo));
            }
            
            //Set the previous step Id
            sortProcess.setPreviousStepId(previousStepNo);
            sortProcess.setProcessType(processType);
            sortProcess.setBaseProductId(baseProductNo);
            sortProcess.setTrackId(trackNo);
            sortProcess.setSortProductName(fabProductName);
            
            //Get the vendorLocations with selected ones if the previous stepId exists
            //else get the vendorLocations from the VendorDAO(with all locations)
            if(previousStepId == null)
                 sortProcess.setVendorLocations(VendorDAO.getSortLocations());
            
            //Get the SortBin Information
            ArrayList bins = new ArrayList();
             int binNo = 1;
            for(int i=0; i<5; i++) {
                SortBins bin = new SortBins("Bin"+binNo," "," ");
                bins.add(bin);
                binNo++;
            }   
            sortProcess.setSortBins(bins);
 
       }
       catch(Exception sqlex) { 
            sqlex.printStackTrace();
            throw new ScalarException ("Could not set SortInformation from database", sqlex);
       }
       
       return sortProcess;
            
   }
 
   /*
    * Get the Process Step Information
    * @param processStepId
    * @return sortProcess
    */
   public static SortProcess getSortProcessInfo(String processStepNo) throws ScalarException
   {
       SortProcess sortProcess = null;
       int processStepId = -1; 
       
       if(processStepNo == null) 
       {
           return null;
       } 
       else 
       {
           processStepId = Integer.parseInt(processStepNo);
           sortProcess = getPreviousSortProcess(processStepId); 
       }
       
       if(sortProcess != null) 
       {
           sortProcess.setProcessStepId(processStepId);
           
           //Set the vendor locations
           sortProcess.setVendorLocations(VendorDAO.getSortLocations(processStepId));
           //set the sort bins
           sortProcess.setSortBins(getSortBins(processStepId));
           // added by ravikanth on 18 Sep 2003 
           //set the dup part numbers
		   sortProcess.setDupProductName(ProductDao.loadProductDupNames( processStepId ));
           
           //get the processtype, baseproduct Id etc...
           //Mallik update this what u need from here ?????
           /*
           sortProcess.setProcessType(ProcessStep.getProcessType(processStepId));
           sortProcess.setBaseProductId(ProcessStep.getBaseProductId(processStepId));
           sortProcess.setSortProductName(ProcessStep.getProductName(processStepId));
           sortProcess.setPreviousStepId(ProcessStep.getPreviousStepId(processStepId));
           */
           //Mallik update this what u need to here ??????
       }
       return sortProcess;
   }
   
   /**
    * Get the SortBins
    * @param processStepId
    * @return SortBins (arrayList)
    */
   public static ArrayList getSortBins(int processStepId) throws ScalarException
   {
       Connection conn = null;
       PreparedStatement pstmt = null;
       ResultSet rs = null;
       ArrayList bins = new ArrayList();
       
       try {
           conn = DataSource.getConnection();
           
           pstmt = conn.prepareStatement(SqlQueryMappings.get("operations.process.sort.loadSortBins"));
           pstmt.setInt (1, processStepId);
           rs = pstmt.executeQuery();

           SortBins sortBin = null;
           while(rs.next())
           {
               sortBin = new SortBins(
                            rs.getString("WSBT_Bin_Number"),
                            rs.getString("WSBT_Bin_Type"),
                            rs.getString("WSBT_Description"));
               
               bins.add(sortBin);
           }
           
       } catch(Exception ex) {
           ex.printStackTrace();
           throw new ScalarException("Could not load bin information for this process step", ex);
       } finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException excp) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException excp) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException excp) {}
            }        
       }
       
       if(bins.isEmpty())
       {
           
             int binNo = 1;
            for(int i=0; i<5; i++) {
                SortBins bin = new SortBins("Bin"+binNo," "," ");
                bins.add(bin);
                binNo++;
            }   
       }
           
       return bins;
   }
   
   
   public static String getAllSortBins(int processStepId) throws ScalarException
   {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String noOfBins = "";

        
        try {
            conn = DataSource.getConnection();
            
            pstmt = conn.prepareStatement (SqlQueryMappings.get ("operations.process.sort.NoOfBins"));
            pstmt.setInt (1, processStepId); // Do we need to set any the database

            rs = pstmt.executeQuery();

            while(rs.next()) {

                noOfBins = rs.getString(1);
            }
                         
        }
        catch (Exception sqlex) {
            sqlex.printStackTrace();
            throw new ScalarException ("Could not get Sort Binning from database", sqlex);
        }
        finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException excp) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException excp) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException excp) {}
            }
        }

        return noOfBins;	
   }   
   
   /**
    * Save the Sort Process Information
    * 
    * @param sortProcess
    */
	public static SortProcess saveSortProcessInfo(SortProcess sortProcess) throws ScalarException
   	{
    	DBTransaction dbTransaction = null;
    	boolean success = false;
       
       	if(sortProcess == null)
       	{
	   		sortProcess = new SortProcess();
       	}
       	try 
       	{
       		int processStepId = sortProcess.getProcessStepId();
            //Begin Transaction
            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();
			
			String[] partNumbers = new String[sortProcess.getDupProductName().length+1];
			partNumbers[0] = sortProcess.getSortProductName();
			for(int i=0; i < sortProcess.getDupProductName().length ; i++)
			{
				partNumbers[i+1] = sortProcess.getDupProductName()[i];
			}
			
            if(processStepId == -1) 
            {
                //Save the details in Process_Step_Table
                processStepId = ProcessStep.getProcessStepID(dbTransaction,sortProcess.getProcessType(),"SORT",sortProcess.getComments());
                sortProcess.setProcessStepId(processStepId);
       
                //Save the details in Product_Table
                ProductDao.saveProcessStepProduct(dbTransaction,processStepId,
                    sortProcess.getSortProductName(),sortProcess.getBaseProductId());
           
                //Update the previous step Info... We don't need this any more as raj written already
                ProductDao.updateNextProcessStepId (
                            dbTransaction, sortProcess.getPreviousStepId(), processStepId);
           
                //Save the details in Program_Doc_table
                if(sortProcess.getProgramDocNo() != "") 
                    sortProcess.setProgramDocId(Upload.getProgramDocID(dbTransaction,"Sort",sortProcess.getProgramDocNo(),
                            sortProcess.getProgramDocFile()));
           
                //Save the details in Procedure_Doc_Table
                if(sortProcess.getProcedureDocNo() != "") 
                    sortProcess.setProcedureDocId(Upload.getProcedureDocID(dbTransaction,"Sort",
                    sortProcess.getProcedureDocNo(),sortProcess.getProgramDocFile()));
           
                //Save the details in Sort_Table
                saveInSortTable(dbTransaction,sortProcess);
                //Save the details in Process_Step_Location_Table
                List vendorLocations = getVendorLocations((ArrayList)sortProcess.getVendorLocations());
                VendorLocationDAO.saveVendorLocations(dbTransaction,processStepId, vendorLocations.iterator());
                
				// added by ravikanth on 18 Sep 2003 
				// update Duplicate part numbers 
				if( sortProcess.getDupProductName() != null && sortProcess.getDupProductName().length > 0 )
				{
					ProductDao.saveProductMap( dbTransaction , processStepId , sortProcess.getDupProductName() , (ArrayList)vendorLocations ); 
				}
            
                List sortBins = (ArrayList)sortProcess.getSortBins();
                saveSortBins(dbTransaction,processStepId,sortBins.iterator());

                // added by hareesh on 2nd October 2003
                // update Sort Process Track to stage Incomplete --> InProcess
                // Instead of having 2 stages, Compelete and Incomplete.
                // It is better to have another stage InProcess
                // Other wise work flow was written like that it is trying
                // to asking user to enter the same data again, inserting a new record into the record,
                // by this an unncessary record is going to be added in to the database previously.
                // this suitiviation araises especially when the user to trying to make the confiration of the Sort
                // and does not enter the values of the Transportion Locations

                ProcessTrackDAO.updateSortAndWaferStoreTrack( dbTransaction, processStepId , sortProcess.getTrackId() , sortProcess.getProcessType(), "InProcess" );

                success = true;
            }
            else
            {
                //System.out.println("In the saving of old one");
				//Update Process Step Table
				ProcessStep.updateProcessStepID(dbTransaction,sortProcess.getProcessType(),"SORT",sortProcess.getComments(),processStepId);	

				//Save the details in Product_Table
                ProductDao.updateProcessStepProduct(dbTransaction,processStepId,
					sortProcess.getSortProductName(),sortProcess.getBaseProductId());

				//Save the details in Program_Doc_table
                if(sortProcess.getProgramDocNo() != "") 
                    sortProcess.setProgramDocId(Upload.getProgramDocID(dbTransaction,"Sort",sortProcess.getProgramDocNo(),
                            sortProcess.getProgramDocFile()));
           
                //Save the details in Procedure_Doc_Table
                if(sortProcess.getProcedureDocNo() != "") 
                    sortProcess.setProcedureDocId(Upload.getProcedureDocID(dbTransaction,"Sort",
                    sortProcess.getProcedureDocNo(),sortProcess.getProgramDocFile()));

				//Update Sort Table
				updateInSortTable(dbTransaction,sortProcess);

				//Update the details in Process_Step_Location_Table
				VendorLocationDAO.deleteVendorLocations(dbTransaction,processStepId);
                List vendorLocations = getVendorLocations((ArrayList)sortProcess.getVendorLocations());
                VendorLocationDAO.saveVendorLocations(dbTransaction,processStepId, vendorLocations.iterator());
				
				// added by ravikanth on 18 Sep 2003 
				// update Duplicate part numbers  
				if( sortProcess.getDupProductName() != null && sortProcess.getDupProductName().length > 0 )
				{
					ProductDao.saveProductMap( dbTransaction , processStepId , sortProcess.getDupProductName() , (ArrayList)vendorLocations ); 
				}				            
				//Delete existing sort bins and Save new sort bins
				deleteSortBins(dbTransaction,processStepId);
				List sortBins = (ArrayList)sortProcess.getSortBins();
                saveSortBins(dbTransaction,processStepId,sortBins.iterator());

				success = true;

            }
       }
       catch (ScalarException se) 
       {
              se.printStackTrace();
       }
       finally {
             // Rollback or Commit the Transaction
             if (success == true) {
                    dbTransaction.commit();
             }
             else {
                   dbTransaction.rollback();
            }
       }
       return sortProcess;
   }
   
   
   // --------------------------------------------------------- Protected Methods
   
   
  /**
   * Save the Sort Bins 
   *
   */
  public static void saveSortBins(DBTransaction dbTransaction, int processStepId, Iterator bins) throws ScalarException
  {
        Connection conn = null;
	PreparedStatement pstmt = null;
	boolean status = false;

	try{
            if (dbTransaction == null) 
                 conn = DataSource.getConnection();
            else 
                 conn = dbTransaction.getConnection();
            

            pstmt = conn.prepareStatement(SqlQueryMappings.get("operations.process.sort.insertSortBins"));
        
            SortBins sortBins = null;

            while(bins.hasNext()){
		sortBins = (SortBins)bins.next(); 
                
                if((!sortBins.getBinType().equals("")) && (sortBins.getBinType().trim().length() != 0)) {
                    pstmt.setInt (1, processStepId);
                    pstmt.setString (2, sortBins.getBinNo());
                    pstmt.setString (3, sortBins.getBinType());
                    pstmt.setString (4, sortBins.getBinDescription());
                
                    pstmt.addBatch();
                }		
            }
	
            pstmt.executeBatch();

            status = true;

	}catch(SQLException sql){
            sql.printStackTrace();
            	throw new ScalarException(" Save Saorting Binning informatin Failed", sql);

	}finally{
            // Commit/Rollback here it selef -- if its not in a manual transaction
            if (dbTransaction == null) {
                 try {
                    if (conn != null) {
                          if (status == true) {
                                conn.commit();
                          }
                          else {
                                conn.rollback();
                          }
                    }
                 }
                 catch (SQLException ignore) {}
            }
                        
            if(pstmt != null){
		try{ pstmt.close(); }catch(Exception e){}
            }
            if(conn != null){
		try{ conn.close(); }catch(Exception e){}
            }

	}
		      
  }

   //Delete Existing Sort bins for existing process step id
	
	public static void deleteSortBins (DBTransaction dbTransaction, int processStepID) throws ScalarException{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		boolean status = false;

		try{
                    // Get the connection from regular datasource if its not in a manual transaction
                    if (dbTransaction == null) {
                        conn = DataSource.getConnection();
                    }
                    else {
                        conn = dbTransaction.getConnection();
                    }

                    pstmt = conn.prepareStatement (SqlQueryMappings.get ("operations.process.sort.deleteSortBins"));
					pstmt.setInt(1,processStepID);
                    pstmt.executeUpdate();
                    status = true;

		}

                catch(Exception sql) {
                    sql.printStackTrace();
                    throw new ScalarException("Could not access VendorLocationList", sql);
		}

                finally{
                    // Commit/Rollback here it selef -- if its not in a manual transaction
                    if (dbTransaction == null) {
                        try {
                            if (conn != null) {
                                if (status == true) {
                                    conn.commit();
                                }
                                else {
                                    conn.rollback();
                                }
                            }
                        }
                        catch (SQLException ignore) {}
                    }
                        
                    if(pstmt != null){
                        try{pstmt.close();}catch(SQLException se){}
                    }
                    if(conn != null){
                        try{ conn.close(); }catch(SQLException se){}
                    }			
		}
	}	
  //Ends Here

  /**
   * Get No Of Sorts for this Product ?? Do we need this
   *
   */
  private static int getNoOfSorts(int baseProductId, String fabProductName) throws ScalarException
  {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int noOfSorts = -1;
        String sqlString = fabProductName+"noOfSorts";
        
        try {
            conn = DataSource.getConnection();
            
            pstmt = conn.prepareStatement (SqlQueryMappings.get ("operations.process.sort.selectNoOfSorts"));
            pstmt.setString (1, sqlString);

            rs = pstmt.executeQuery();

            if (rs.next() == false) {
                //There is no values found with this...
            }
            else {
                noOfSorts = rs.getInt("Field_Name");
            }
    
        }
        catch (Exception sqlex) {
            sqlex.printStackTrace();
            throw new ScalarException ("Could not get no.of sorts from database", sqlex);
        }
        finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException excp) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException excp) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException excp) {}
            }
        }

        return noOfSorts;
  }
   
     
  /**
   * Get the previous step informatin ..
   *
   * @param previousStepId
   * 
   * @return SortProcess
   */
   private static SortProcess getPreviousSortProcess(int previousStepId) throws ScalarException
   {
       
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        SortProcess sortProcess = null;
        
        try {
            
            conn = DataSource.getConnection();

            pstmt = conn.prepareStatement (SqlQueryMappings.get ("operations.process.sort.selectPreviousSort"));
            pstmt.setInt (1, previousStepId);
            
            rs = pstmt.executeQuery();
            if (rs.next() == false) {
                //There is no values found with this...
                //System.out.println("Not found ");
            }
            else {
                sortProcess = new SortProcess();
                
                //sortProcess.setProcessStepId(previousStepId);
                sortProcess.setExpectedNetDiePerWafer(rs.getInt("ST_Expected_Net_Die_Wafer"));
                sortProcess.setLotHoldYieldLimit(rs.getFloat("ST_Lot_Hold_Yield_Limit"));
                sortProcess.setLotScrapYieldLimit(rs.getFloat("ST_Lot_Scrap_Yield_Limit"));
                sortProcess.setStndUnitCost(rs.getFloat("ST_Standard_Unit_Cost"));
                sortProcess.setPoFrequency(rs.getString("ST_PO_Frequency"));
                sortProcess.setProbeCardNo(rs.getString("ProbeCard_ID"));
                sortProcess.setSystem(rs.getString("ST_System"));
                sortProcess.setRmaCriteria(rs.getString("ST_RMA_Criteria"));
                sortProcess.setScrapCriteria(rs.getString("ST_Scrap_Criteria"));   
                sortProcess.setProgramDocNo(rs.getString("PDT_Doc_Number"));
                sortProcess.setProcedureDocNo(rs.getString("Procedure_Doc_Number"));
                sortProcess.setSortProductName(rs.getString("PArt_Number"));
                sortProcess.setBaseProductId(rs.getInt("P_Parent_Product_ID"));
                sortProcess.setProcessType(rs.getString("PS_Type"));
                sortProcess.setComments(rs.getString("PS_Comments"));
                sortProcess.setPreviousStepId(rs.getInt("previousStep_ID"));
                                
             }
    
        }
        catch (Exception sqlex) {
            sqlex.printStackTrace();
            throw new ScalarException ("Could not getSort Information from database", sqlex);
        }
        finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException excp) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException excp) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException excp) {}
            }
        }
        return sortProcess;
       
   }   
   
                       
  /**
   * Save Sort Table
   *
   *
   */
   public static void saveInSortTable(DBTransaction dbTransaction, SortProcess sortProcess) throws ScalarException
   {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        boolean success = false;
        
        try {
            // Get the connection from regular datasource if its not in a manual transaction
            if (dbTransaction == null) {
                conn = DataSource.getConnection();
            }
            else {
                conn = dbTransaction.getConnection();
            }
            //System.out.println("process stepId " + sortProcess.getProcessStepId());
            pstmt = conn.prepareStatement (SqlQueryMappings.get ("operations.process.sort.insertInSortTable"));
            
            pstmt.setInt (1, sortProcess.getProcessStepId());
            pstmt.setFloat (2, sortProcess.getExpectedNetDiePerWafer());
            pstmt.setFloat (3, sortProcess.getLotHoldYieldLimit());
            pstmt.setFloat (4, sortProcess.getLotScrapYieldLimit());
            pstmt.setFloat (5, sortProcess.getStndUnitCost());
            pstmt.setString (6, sortProcess.getPoFrequency());
            pstmt.setInt (7, sortProcess.getProcedureDocId());
            pstmt.setInt (8, sortProcess.getProgramDocId());
            pstmt.setString (9, sortProcess.getProbeCardNo());
            pstmt.setString (10, sortProcess.getSystem());
            pstmt.setString (11, sortProcess.getRmaCriteria());
            pstmt.setString (12, sortProcess.getScrapCriteria());

            pstmt.executeUpdate();
                        
            success = true;
        }
        catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not Insert Sort Info into database", sqle);
        }
        finally {
            if (dbTransaction == null) {
                try {
                    if (conn != null) {
                        if (success == true) 
                            conn.commit();
                        else 
                            conn.rollback();
                    }
                }
                catch (SQLException ignore) {}
            }

            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore){}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore){}
            }
        }

   }


   //Update sort process added By Raj Vallamsetla
	public static void updateInSortTable(DBTransaction dbTransaction, SortProcess sortProcess) throws ScalarException
   {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        boolean success = false;
        
        try {
            // Get the connection from regular datasource if its not in a manual transaction
            if (dbTransaction == null) {
                conn = DataSource.getConnection();
            }
            else {
                conn = dbTransaction.getConnection();
            }
            //System.out.println("process stepId " + sortProcess.getProcessStepId());
            pstmt = conn.prepareStatement (SqlQueryMappings.get ("operations.process.sort.updateInSortTable"));
            
            pstmt.setFloat (1, sortProcess.getExpectedNetDiePerWafer());
            pstmt.setFloat (2, sortProcess.getLotHoldYieldLimit());
            pstmt.setFloat (3, sortProcess.getLotScrapYieldLimit());
            pstmt.setFloat (4, sortProcess.getStndUnitCost());
            pstmt.setString (5, sortProcess.getPoFrequency());
            pstmt.setInt (6, sortProcess.getProcedureDocId());
            pstmt.setInt (7, sortProcess.getProgramDocId());
            pstmt.setString (8, sortProcess.getProbeCardNo());
            pstmt.setString (9, sortProcess.getSystem());
            pstmt.setString (10, sortProcess.getRmaCriteria());
            pstmt.setString (11, sortProcess.getScrapCriteria());
			pstmt.setInt (12, sortProcess.getProcessStepId());

            pstmt.executeUpdate();
                        
            success = true;
        }
        catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not Insert Sort Info into database", sqle);
        }
        finally {
            if (dbTransaction == null) {
                try {
                    if (conn != null) {
                        if (success == true) 
                            conn.commit();
                        else 
                            conn.rollback();
                    }
                }
                catch (SQLException ignore) {}
            }

            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore){}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore){}
            }
        }

   }

  // Ends Here




   private static List getVendorLocations(ArrayList list)
   {
       List locations = new ArrayList();
       
       for(int i=0; i<list.size(); i++)
       {
           VendorLocation location = (VendorLocation) list.get(i);

           if((location.getSelect() != "") || (!location.getSelect().equals("")))
           {    
               VendorLocation selectedLocation = new VendorLocation(location.getPriority(),
                                                 location.getLocationName(),
                                                 location.getLocationID(),
                                                 location.getSplit(),
                                                 location.getCapacityWeek(),
                                                 location.getNormalCost(),
                                                 location.getExpressCost(),
                                                 location.getNormalCycleTime(),
                                                 location.getExpressCycleTime());
               
                selectedLocation.setLaserRepairAllowed(location.getLaserRepairAllowed() ? true : false);
                selectedLocation.setLaserRepairCost(location.getLaserRepairCost());
                selectedLocation.setSelect (location.getSelect());
                locations.add(selectedLocation);
           }
       }
       
       return locations;
       
   }
  
}